package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class MallOrderAdapter extends BaseAdapter {

    private Context mContext;

    private List<OrderInfo> mList = new ArrayList<OrderInfo>();

    public MallOrderAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<OrderInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.mall_order_item, parent, false);

            childHolder.mTradeTime = (TextView) convertView.findViewById(R.id.trade_time);
            childHolder.goodImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.goodName = (TextView) convertView.findViewById(R.id.goodsname);
            childHolder.needFlour = (TextView) convertView.findViewById(R.id.need_flour);
//            childHolder.tradeState = (TextView) convertView.findViewById(R.id.trade_state);
//            childHolder.payBtn = (ImageView) convertView.findViewById(R.id.paynow_btn);
            childHolder.mNickName = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.mPhone = (TextView) convertView.findViewById(R.id.phone);
            childHolder.mAddress = (TextView) convertView.findViewById(R.id.sh_addr);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        OrderInfo info = mList.get(position);
        if (info != null) {
            final String videologo = info.getGoodsImage();
            if (videologo != null && videologo.length() > 0) {
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.goodImg, videologo, new ViewUtil.Size(ViewUtil.dip2px(mContext, 50), ViewUtil.dip2px(mContext, 50)));
            }
            childHolder.mTradeTime.setText(info.getDealDate());
            childHolder.goodName.setText(info.getGoodsName());
            childHolder.needFlour.setText(info.getGoodsPrice());
            childHolder.mNickName.setText("收货人:" + info.getAddressUser());
            childHolder.mPhone.setText(info.getMobile());
            childHolder.mAddress.setText("收货地址:" + info.getAddress());
        }


        return convertView;
    }

    class ChildHolder {
        TextView mTradeTime;
        ImageView goodImg;
        TextView goodName;
        TextView needFlour;
        //        TextView tradeState;
//        ImageView payBtn;
        TextView mNickName;
        TextView mPhone;
        TextView mAddress;
    }
}
